package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptionResolver.ExceptionMessage;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @RequestMapping("/add")
    @ExceptionMessage(value = MessageConstant.ADD_MENU_FALL)
    public Result add(@RequestBody Menu menu) {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    @RequestMapping("/del")
    @ExceptionMessage(value = MessageConstant.DEL_MENU_FALL)
    public Result del(Integer[] ids) {
            menuService.del(ids);
            return new Result(true, MessageConstant.DEL_MENU_SUCCESS);
    }

    @RequestMapping("update")
    @ExceptionMessage(value = MessageConstant.EDIT_MENU_FALL)
    public Result update(@RequestBody Menu menu) {
            menuService.update(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }
    //查询单个菜单
    @RequestMapping("find")
    @ExceptionMessage(value = MessageConstant.GET_MENU_FAIL)
    public Result find(@RequestBody Menu menu) {
            Menu one = menuService.findOne(menu);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, one);
    }
    //查询所有
    @RequestMapping("findAll")
    @ExceptionMessage(value = MessageConstant.GET_MENULIST_FALL)
    public Result findAll() {
            List<Menu> menus = menuService.findAll();
            return new Result(true, MessageConstant.GET_MENULIST_SUCCESS, menus);
    }
    //分页查询一级目录
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
            PageResult page = menuService.findPage(queryPageBean);
            return page;
    }

    @RequestMapping("/getMenuList")
    @ExceptionMessage(value = MessageConstant.GET_MENU_FAIL)
    public Result getMenuList(String username) {
            List<Menu> menuList = menuService.getMenuList(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
    }
}
